from pyfpdf import fpdf
from support import map,join
from support import pdf as pdfData
import os



def outputToPDF(stats,filename):	
	"""
	Provides PDF output for the Log Analyzer statistics object
	
	Keyword arguments:
    stats -- statistics object
	filename -- path to output pdf
	
	"""
	
	pdf = pdfData.MyPDF()
	pdf.alias_nb_pages()
	pdf.add_page()
	pdf.set_font("Arial", size=18)
	pdf.cell(200, 10, txt="Packet Statistics", align="C")
	pdf.set_font("Arial",'B',12)
	
	packetsIn, packetsOut, errors = stats.getNumbers()
	totalPackets = packetsIn + packetsOut + errors
	
	pdf.write(12,"\nNumber Of Packets Processed: " + str(totalPackets) + " Packets\n")
	pdf.set_font("Arial", size=10)
	pdf.write(8,"Packet Direction:")
	pdf.write(8,"\n  IN: " + str(packetsIn))
	pdf.write(8,"\n  OUT: " + str(packetsOut))
	pdf.write(8,"\n  ERRORS: " + str(errors))
	pdf.write(8,"\n")
	pdf.set_font("Arial",'B',12)
	pdf.write(12,"\nStatistics by Country:\n")
	pdf.set_font("Arial", size=10)
	
	countryList = stats.getCountryStats()
	for x in countryList.keys():
		pdf.write(8,'  ' + str(x) + ' - ' + str(countryList[x]) + ' Packets\n')
		
	pdf.add_page()
	pdf.set_font("Arial",'B',12)	
	pdf.write(12,"\nStatistics by City:\n")
	pdf.set_font("Arial", size=10)
	
	cityList = stats.getCityStats()
	for x in cityList.keys():
		pdf.write(8,'  ' + str(x) + " (Long: "+ str(cityList[x][1]) + " Lat: "+ str(cityList[x][2]) + ') - ' + str(cityList[x][0]) + ' Packets\n')	
	
	pdf.add_page()
	pdf.set_font("Arial",'B',12)	
	pdf.write(12,"\nStatistics by Date:\n")
	pdf.set_font("Arial", size=10)
	
	dateList = stats.getPacketsByDate()
	for x in dateList.keys():
		pdf.write(8,'  ' + str(x) + ' - ' + str(dateList[x]) + ' Packets\n')	
	
	
	pdf.add_page()
	pdf.set_font("Arial",'B',12)	
	pdf.write(12,"\nIP's Blocked:\n")
	pdf.set_font("Arial", size=10)

	ips = stats.getIPStats()
	for w in sorted(ips, key=ips.get, reverse=True):
		pdf.write(8,'  ' + str(w) + ' - ' + str(ips[w]) + ' Times\n')

	pdf.add_page()
	pdf.set_font("Arial",'B',12)	
	pdf.write(12,"\nMAC's Blocked:\n")
	pdf.set_font("Arial", size=10)
			
	macs = stats.getMACStats()
	for w in sorted(macs, key=macs.get, reverse=True):
		pdf.write(8,'  ' + str(w) + ' - ' + str(macs[w]) + ' Times\n')
	print '\n'
	
	pdf.output("statsresults.pdf")

	try:
		print "\nCreating Location Map PDF:...."
		pdfMap = map.DrawMap("Packet Location Map",cityList)
		pdfMap.createPDF()
		
		statmap = map.statistics(stats)
		statmap.statsPDF()

		join.join(filename)
		
		print "Done.\n"	
	except:
		os.rename("statsresults.pdf",filename)
		print " No X server installed. Skipping MAP PDF Generation"

	
def outputToCMD(stats):
	"""
	Provides stdout output for the Log Analyzer statistics object
	
	
    :param stats: statistics object
	
	"""
	
	packetsIn, packetsOut, errors = stats.getNumbers()
	totalPackets = packetsIn + packetsOut + errors
		
	print "\nNumber Of Packets Processed: " + str(totalPackets) + " Packets\n"   		
	print "Packet Direction:"
	print "  IN: " + str(packetsIn)
	print "  OUT: " + str(packetsOut)
	print "  ERRORS: " + str(errors)
	print "\n"

	print "\nStatistics by Date:"
	dateList = stats.getPacketsByDate()
	for x in dateList.keys():
		print '  ' + str(x) + ' - ' + str(dateList[x]) + ' Packets'

	print "\nStatistics by Country:"
	countryList = stats.getCountryStats()
	for x in countryList.keys():
		print '  ' + str(x) + ' - ' + str(countryList[x]) + ' Packets'
	
	print "\nStatistics by City:"
	cityList = stats.getCityStats()
	for x in cityList.keys():
		print '  ' + str(x) + " (Long: "+ str(cityList[x][1]) + " Lat: "+ str(cityList[x][2]) + ') - ' + str(cityList[x][0]) + ' Packets'  
		
	print "\nTop10 IP's Blocked:"
	ips = stats.getIPStats()
	i=0
	for w in sorted(ips, key=ips.get, reverse=True):
		if i < 10:
			print '  ' + str(w) + ' - ' + str(ips[w]) + ' Times'
		i += 1
		
	print "\nTop10 MAC's Blocked:"
	macs = stats.getMACStats()
	i=0
	for w in sorted(macs, key=macs.get, reverse=True):
		if i < 10:
			print '  ' + str(w) + ' - ' + str(macs[w]) + ' Times'
		i += 1
	print '\n'
